<template>
  <div class="addTeacher">
    <h3 class="title">添加教师信息</h3>
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="150px"
      class="demo-ruleForm"
    >
      <!-- 下面是表单的每一项 -->
      <el-form-item label="姓名" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-radio-group v-model="ruleForm.sex">
          <el-radio label="男"></el-radio>
          <el-radio label="女"></el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="科目" prop="type">
        <el-input v-model="ruleForm.type"></el-input>
      </el-form-item>
      <el-form-item label="教师描述" prop="description">
        <el-input v-model="ruleForm.description"></el-input>
      </el-form-item>
      <el-form-item label="教师编号" prop="teacherNo">
        <el-input v-model="ruleForm.teacherNo"></el-input>
      </el-form-item>
      <el-form-item label="年龄" prop="age">
        <el-input v-model="ruleForm.age"></el-input>
      </el-form-item>
      <el-form-item label="登陆密码" prop="passWord">
        <el-input
          type="passWord"
          autocomplete="off"
          v-model="ruleForm.passWord"
        ></el-input>
      </el-form-item>

      <el-upload
        class="avatar-uploader"
        :data="data"
        :action="action"
        :headers="headers"
        :show-file-list="false"
        :on-success="handleAvatarSuccess"
        :before-upload="beforeAvatarUpload"
      >
        <img v-if="imageUrl" :src="imageUrl" class="avatar" />
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        <div class="el-upload__text">点击上传头像</div>
      </el-upload>

      <!-- 下面是表单的提交和重置按钮 -->
      <el-form-item>
        <el-button type="primary" :plain="true" @click="submitForm('ruleForm')"
          >保存</el-button
        >
        <el-button type="info" :plain="true" @click="resetForm('ruleForm')"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import adminRequest from "../request/admin";
import axios from "../request/axios";
import cookie from "js-cookie";
export default {
  data() {
    var name = (rule, value, callback) => {
      if (value === "") {
        return callback(new Error("请填写名称"));
      } else {
        return callback();
      }
    };
    var sex = (rule, value, callback) => {
      if (value === "") {
        return callback(new Error("请选择性别"));
      } else {
        return callback();
      }
    };
    var age = (rule, value, callback) => {
      if (value === "") {
        return callback(new Error("请填写年龄"));
      } else {
        return callback();
      }
    };
    var passWord = (rule, value, callback) => {
      if (value === "") {
        return callback(new Error("请填写密码"));
      } else {
        return callback();
      }
    };
    var type = (rule, value, callback) => {
      if (value === "") {
        return callback(new Error("请填写所教科目"));
      } else {
        return callback();
      }
    };
    var teacherNo = (rule, value, callback) => {
      if (value === "") {
        return callback(new Error("请填写教师编号"));
      } else {
        return callback();
      }
    };
    var description = (rule, value, callback) => {
      if (value === "") {
        return callback(new Error("请填写教师的描述"));
      } else {
        return callback();
      }
    };

    return {
      data: {
        dataId: Math.random().toString(36).substr(3, 6) + Date.now().toString(),
      },
      action: axios.defaults.baseURL + "/file/upload",
      imageUrl: "",
      headers: {
        satoken: cookie.get("satoken"),
      },

      ruleForm: {
        name: "",
        sex: "",
        age: "",
        passWord: "",
        type: "",
        description: "",
        teacherNo: "",
      },
      // 下面是element表单自带的验证规则
      rules: {
        name: [{ validator: name, trigger: "blur" }],
        sex: [{ validator: sex, trigger: "blur" }],
        age: [{ validator: age, trigger: "blur" }],
        passWord: [{ validator: passWord, trigger: "blur" }],
        type: [{ validator: type, trigger: "blur" }],
        description: [{ validator: description, trigger: "blur" }],
        teacherNo: [{ validator: teacherNo, trigger: "blur" }],
      },
    };
  },

  methods: {

    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
    },

    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },

    submitForm(ruleForm) {
      this.$refs[ruleForm].validate((valid) => {
        if (valid) {
          adminRequest
            .editTeacher({
              name: this.ruleForm.name,
              sex: this.ruleForm.sex,
              age: this.ruleForm.age,
              passWord: this.ruleForm.passWord,
              type: this.ruleForm.type,
              description: this.ruleForm.description,
              teacherNo: this.ruleForm.teacherNo,
              photo: this.data.dataId
            })
            .then((res) => {
              // 如果请求成功执行的操作
              if (res.data.success == true) {
                this.$message.success("添加成功!");
                this.resetForm();
                this.clearComp();
                // 请求失败显示失败的提示语
              } else {
                this.$message.error(res.data.message);
              }
            });
          //下面是如果表单信息不完整 才能执行的操作（element自带的判断）
        } else {
          this.$message.error("请检查信息是否完整！");
          return false;
        }
      });
    },
    // 清空表单数据（element自带的判断）
    resetForm() {
      this.$refs.ruleForm.resetFields();
    },
  },

  created() {
    console.log(this.item);
  },
};
</script>

<style lang="scss" scoped>
// 下面是页面结构的css样式 （这里样式比较少就不用放在另外的文件引入了）
.addTeacher {
  // overflow: hidden;

   .title {
    color: #000;
    text-align: center;
    font-size: 20px;
    padding-top: 30px;
  }
  ::v-deep .demo-ruleForm {
    width: 500px;
    margin: 30px auto;
  }
  ::v-deep .el-input__inner {
    height: 45px;
    font-size: 16px;
  }
  ::v-deep .el-form-item__label {
    font-size: 16px;
    position: relative;
    top: 10px;
  }
  ::v-deep .el-form-item__content {
    margin-top: 10px;
    height: 50px;
    display: flex;
  }
  ::v-deep .el-form-item__error {
    font-size: 16px;
  }

  ::v-deep .el-select-dropdown__item {
    display: flex;
    justify-content: center;
  }
  ::v-deep .el-radio-group {
    width: 100px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-top: 11px;
  }

  ::v-deep .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    margin-left: 150px;
    height: 178px;
  }
  ::v-deep .avatar-uploader .el-upload:hover {
    border-color: #409eff;
  }
  ::v-deep .avatar-uploader-icon {
    font-size: 24px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  ::v-deep .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
  ::v-deep .el-upload__text {
    color: #8c939d;
    position: relative;
    bottom: 50px;
    font-size: 14px;
  }
  ::v-deep .el-form-item__label{
    color: #000;
  }
}
</style>